package com;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import jdk.nashorn.internal.objects.annotations.Getter;

import java.sql.Time;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicStampedReference;


public class TestCountDownLatch {
    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        //  testCountDownLatch();
        // testCyclicBarriar();
      /*  AtomicStampedReference<Integer> integerAtomicStamped =
                 new AtomicStampedReference<>(1, 0);
        integerAtomicStamped.compareAndSet(1,2,0,1);
        System.out.println(integerAtomicStamped.getStamp());
        System.out.println(integerAtomicStamped.getReference());*/
        Country[] values = Country.values();
        for (Country value : values) {
            System.out.println(value.getRtn());
            System.out.println(value.getMsg());
        }
    }

    private static void testCyclicBarriar() throws InterruptedException, BrokenBarrierException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                try {
                    TimeUnit.SECONDS.sleep(2);
                    System.out.println("大家一起等一等啊");
                    cyclicBarrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
        System.out.println("我在等什么呢？");
        cyclicBarrier.await();
        System.out.println("全都执行完成了");
    }

    // 测试 CountDownLatch 类
    private static void testCountDownLatch() {
        CountDownLatch countDownLatch = new CountDownLatch(5);
        for (int i = 0; i < 5; i++) {
            final int  tempId = i;
            new Thread(()->{
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                }
                countDownLatch.countDown();
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("终究是走到这里了"+tempId);
            },i+"").start();
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("终于能够执行了");
    }
}
 enum Country{
    ONE  (1,"齐"),TWO(2,"赵");

     public Integer getRtn() {
         return rtn;
     }

     public String getMsg() {
         return msg;
     }

     Country(Integer rtn, String msg) {
         this.rtn = rtn;
         this.msg = msg;
     }
     private Integer rtn ;
     private String msg;

}
